import {getPath} from "./env";
import Vue from 'vue';

/**
 *
 * 手机号验证
 * @param {*} telNumber   手机号
 */

export function validateTel(telNumber) {
  const reg = /^[1-9]\d{10}$/;
  return reg.test(telNumber);
}

/**
 *
 * 身份证验证
 * @param {*} idNumber   身份证
 */

export function idNumber(idNumber) {
  const reg = /^\d{15}|\d{18}$/;
  return reg.test(idNumber);
}

/**
 *
 * 密码验证
 * @param {*} mmyzT   密码
 */

export function mmyzT(mmyzT) {
  const reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,10}$/;
  return reg.test(mmyzT);
}

/**
 * 消息提示框
 * @param {*} vueobj
 * @param {*} opt
 */
export function errTip(vueobj, opt) {
  vueobj.$message({
    showClose: true,
    message: opt.message,
    type: opt.type
  });
}

/**
 * 文件上传状态
 * status 1 是
 */

export function fileStatus(status) {
  if (status === 1) {
    return "是"
  } else {
    return "否"
  }
}

/**
 * 文件备注
 * 如果空则输出无
 */

export function fileRemark(remark) {
  if (remark == null) {
    return "无"
  }
}

/**
 * 获取项目信息地区
 */

export function getArea(area) {
  switch (area) {
    case "BS":
      return "宝山区";
    case "CM":
      return "崇明区";
    case "CN":
      return "长宁区";
    case "FX":
      return "奉贤区";
    case "HK":
      return "虹口区";
    case "HP":
      return "黄浦区";
    case "JA":
      return "静安区";
    case "JD":
      return "嘉定区";
    case "JS":
      return "金山区";
    case "KQ":
      return "跨区区";
    case "MH":
      return "闵行区";
    case "PD":
      return "浦东区";
    case "PT":
      return "普陀区";
    case "QP":
      return "青浦区";
    case "SJ":
      return "松江区";
    case "SW":
      return "市境外";
    case "XH":
      return "徐汇区";
    case "YP":
      return "杨浦区";
    case "ZB":
      return "闸北区";
  }
}

/**
 * 从时间戳到时间
 * @param timestamp
 * @returns {string}
 */

export function timestampToTime(timestamp) {
  function paseDateSingular(time) {
    if (time < 10) {
      return "0" + time
    } else {
      return time
    }
  }

  const dates = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const DY = dates.getFullYear() + '-',
    DM = paseDateSingular(dates.getMonth() + 1) + '-',
    DD = paseDateSingular(dates.getDate()) + ' ',
    Dh = paseDateSingular(dates.getHours()) + ':',
    Dm = paseDateSingular(dates.getMinutes());
  return DY + DM + DD + Dh + Dm;
}

export function timestampToDate(timestamp) {

  function paseDate(time) {
    if (time < 10) {
      return "0" + time
    } else {
      return time
    }
  }

  if (timestamp) {
    let dates = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let year = dates.getFullYear() + '-',
      month = paseDate(dates.getMonth() + 1) + '-',
      day = paseDate(dates.getDate()) + ' ';
    return year + month + day;
  } else {
    return null;
  }

}

/**
 * 从时间到时间戳
 * @param time
 * @returns {string}
 */

export function timeToTimestamp(time) {
  const date = new Date(time);
  const timeestamp = Date.parse(date);
  const timeestamps = toString(timeestamp);
  return timeestamps;
}


export var routerprefix = "/ui";

/**
 * 设计方案状态
 * @param status
 * @returns {string}
 */

export function showDesignStatus(status) {
  var num = String(status);
  num = num.substring(3);
  switch (num) {
    case "01":
      return "设计方案填报";
    case "02":
      return "通知设计单位传图";
    case "03":
      return "图纸上传";
    case "04":
      return "申请提交待接受";
    case "05":
      return "已接收审批中";
    case "06":
      return "待补正提交";
    case "07":
      return "已接收补正审批中";
    case "08":
      return "受理中";
    case "09":
      return "设计方案不受理";
    case "10":
      return "设计方案公示中";
    case "11":
      return "设计方案专家评审中";
    case "12":
      return "退回修改提交";
    case "13":
      return "退回修改已接收";
    case "14":
      return "结果录入中";
    case "15":
      return "设计方案审批通过";
    case "16":
      return "设计方案审批不通过";
  }
}

/**
 * 竣工首页状态
 * @param stageId
 * @returns {string}
 */


export function getStageAcceptance(stageId) {
  switch (stageId) {
    case 60101:
      return '验收申请申请表填写中';
    case 60102:
      return '验收通知相关单位传图';
    case 60103:
      return '验收通知相关单位传图完毕';
    case 60104:
      return '验收提交待接收';
    case 60105:
      return '建设部门已接收';
    case 60106:
      return '等待建设单位补正';
    case 60107:
      return '已接收补正提交';
    case 60108:
      return '受理';
    case 60109:
      return '不受理退回重新方案申请';
    case 60110:
      return '等待建设单位退回整改提交';
    case 60111:
      return '已接收退回整改';
    case 60117:
      return '审批通过';
    case 60118:
      return '审批不通过';
  }

}


/**
 * 施工许可状态
 * @param status
 * @returns {string}
 */

export function showConstructionStatus(status) {
  var num = String(status);
  num = num.substring(3);
  switch (num) {
    case "01":
      return "施工许可填报";
    case "02":
      return "提交测绘落图";
    case "03":
      return "提交测绘落图";
    case "04":
      return "通知设计单位传图";
    case "05":
      return "图纸上传";
    case "06":
      return "施工许可已提交待接收";
    case "07":
      return "已接收审批中";
    case "08":
      return "待补正";
    case "09":
      return "已接收审批中";
    case "10":
      return "已接收审批中";
    case "11":
      return "施工许可不受理";
    case "15":
      return "退回修改待提交";
    case "16":
      return "已接收退回修改";
    case "17":
      return "已接收审批中";
    case "18":
      return "已接收审批中";
    case "19":
      return "已接收审批中";
    case "20":
      return "已接收审批中";
    case "21":
      return "已接收审批中";
    case "22":
      return "已接收审批中";
    case "23":
      return "已接收审批中";
    case "24":
      return "已接收审批中";
    case "25":
      return "已接收审批中";
    case "26":
      return "施工许可证核发";
    case "27":
      return "施工许可不通过";
  }
}

/**
 *
 * @param stageids
 * @returns {string}
 */

export function getStageid(stageids) {
  switch (stageids) {
    case 101:
      // return "项目报建申请";
      return "报送中";
    case 102:
      // return "完成报建表";
      return "报送中";
    case 103:
      // return "报建表已提交";
      return "报送中";
    case 104:
      // return "报建表已报建";
      return "报送完成";
    case 20:
      return "方案设计";
    case 20101:
      return "方案申请申请表填写中";
    case 20102:
      return "方案提交并通知设计单位上传图纸";
    case 20103:
      return "设计单位设计图纸上传完成";
    case 20104:
      return "等待规划部门待接收";
    case 20105:
      return "规划部门已接收并分发";
    case 20106:
      return "等待建设单位补正提交";
    case 20107:
      return "已接收补正";
    case 20108:
      return "受理";
    case 20109:
      return "不受理 退回重新方案申请";
    case 20110:
      return "公示中";
    case 20111:
      return "专家评审中";
    case 20112:
      return "等待建设单位退回修改提交";
    case 20113:
      return "已接收退回修改";
    case 20114:
      return "规划结果录入";
    case 20115:
      return "审批通过";
    case 20116:
      return "审批不通过";
    case 30101:
      return "施工申请";
    case 30102:
      return "提交测绘落图";
    case 30103:
      return "测绘院已落图";
    case 30104:
      return "施工许可申请已提交并通知设计单位上传图纸";
    case 30105:
      return "资料上传完成并已经通知建设单位提交建设部门";
    case 30106:
      return "待接收";
    case 30107:
      return "已接收";
    case 30108:
      return "待补正";
    case 30109:
      return "已接收补正";
    case 30110:
      return "施工许可受理";
    case 30111:
      return "施工许可不受理";
    case 30115:
      return "退回修改";
    case 30116:
      return "已接收退回修改";
    case 30117:
      return "施工图已接收（审图）";
    case 30118:
      return "施工图申请复核（审图）";
    case 30119:
      return "施工图复核完成（审图）";
    case 30120:
      return "施工图审查完成（审图）";
    case 30121:
      return "施工图审查合格（审图）";
    case 30122:
      return "施工图审批通过";
    case 30123:
      return "施工图审批不通过";
    case 30124:
      return "规划审批通过";
    case 30125:
      return "规划审批不通过";
    case 30126:
      return "建筑施工审批通过";
    case 30127:
      return "建筑施工审批不通过";
  }
}

export function showStatus(status) {
  switch (status) {
    case '1':
      return "待提交";
    case '2':
      return "待接收";
    case '3':
      return "已接收";
    case '4':
      return "待补正";
    case '5':
      return "补正后待接收";
    case '6':
      return "已回复";
    default :
      return "非法状态";
  }
}

//获取监管意见状态
export function getsupervisionStatus(status) {
  switch (status) {
    case 0:
      return "保存未提交";
    case 1:
      return "提交待建设单位回复";
    case 2:
      return "建设单位已回复待确认";
    case 3:
      return "已确认已解决";
    case 4:
      return "已确认未解决";
  }
}


//合并对象属性
export function extend(target, source) {
  for (var obj in source) {
    target[obj] = source[obj];
  }
  return target;
}


/**
 * 在未知对象索引情况下，删除数组对象中某个值为value的对象
 * @param targetArray
 * @param proname
 * @param value
 * @param replaceObj
 * @returns {number}
 */
export function delArrayObject(targetArray, proname, value, replaceObj) {
  var targetIndex = -1;
  if (targetArray) {
    for (let i = 0; i < targetArray.length; i++) {
      if (targetArray[i][proname] == value) {
        targetIndex = i;
        break;
      }
    }
    if (targetIndex >= 0) {
      if (replaceObj) {
        targetArray.splice(targetIndex, 1, replaceObj);
      } else {
        targetArray.splice(targetIndex, 1);
      }
    }
  }
  return targetIndex;
}

/**
 * 拷贝数组(浅拷贝)
 *
 * 参数1,需要拷贝的数组（ type array ）
 * 参数2,定义变量名 （ type string ）
 * @param obj
 * @param copy
 * @constructor
 */

export function Copy(obj, copy) {
  if (Array.isArray(obj) === true) {
    window[copy] = [];
    for (let i = 0; i < obj.length; i++) {
      window[copy].push(obj[i]);
    }
  } else if (typeof obj === 'string') {
    alert(typeof obj);
  } else if (typeof obj === 'number') {
    alert(typeof obj);
  } else if (typeof obj === undefined) {
    console.error('utils.js工具类 Copy方法参数1 未定义!');
  } else if (typeof obj === 'boolean') {
    alert(typeof obj);
  } else {
    window[copy] = {};
    window[copy + 'keys'] = [];
    window[copy + 'values'] = [];
    for (let i in obj) {
      window[copy + 'keys'].push(i);
    }
    for (let i = 0; i < window[copy + 'keys'].length; i++) {
      let j = obj[window[copy + 'keys'][i]];
      window[copy + 'values'].push(j);
    }
    for (let i = 0; i < window[copy + 'keys'].length; i++) {
      window[copy][window[copy + 'keys'][i]] = window[copy + 'values'][i];
    }
  }
}

/**
 * 工厂模式 (设计方案)
 * 添加道路机动车入口
 * @constructor
 */

export function FactoryModel(roadNameKey, roadNameValue, roadWidthKey, roadWidthValue) {
  let obj = {
    [roadNameKey]: roadNameValue,
    [roadWidthKey]: roadWidthValue
  };
  return obj;
}

/**
 * 工厂模式 (竣工验收/添加老道路)
 * 添加道路机动车入口
 * @constructor
 */

export function FactoryModelAccOld(historyName, historyValue, roadWidthKey, roadWidthValue) {
  let obj = {
    [historyName]: historyValue,
    [roadWidthKey]: roadWidthValue,
  };
  return obj;
}

/**
 * 工厂模式 (竣工验收)
 * 添加道路机动车入口
 * @constructor
 */

export function FactoryModelAcc(historyName, historyValue, roadNameKey, roadNameValue, roadWidthKey, roadWidthValue) {
  let obj = {
    [historyName]: historyValue,
    [roadNameKey]: roadNameValue,
    [roadWidthKey]: roadWidthValue,
  };
  return obj;
}


/**
 * 删减对象
 * @param obj1
 * @param obj2
 * @param isnumber
 */

export function delSurplus(obj1, obj2, isnumber) {
  let arr1 = [];
  let arr2 = [];
  for (let i in obj2) {
    arr1.push(i);
  }
  for (let i = 0; i < arr1.length; i++) {
    arr2.push(obj1[arr1[i]]);
  }
  let obj = {};

  if (isnumber) {
    for (let i = 0; i < arr1.length; i++) {
      obj[arr1[i]] = arr2[i] === undefined || arr2[i] === null ? 0 : arr2[i];
    }
  } else {
    for (let i = 0; i < arr1.length; i++) {
      obj[arr1[i]] = arr2[i] === undefined ? null : arr2[i];
    }
  }
  return obj;
}

/**
 * 项目分类截取
 * @param str
 */

export function protypeStr(str) {
  // let arr = str.split(',');
  let arr = str?str.split(','):null;
  return protype(arr);
}

function protype(arr) {
  // let array = arr[0].toString();
  let array = arr ? arr[0].toString(): null;
  switch (array) {
    case '070001':
      return '邮电通信及广电项目';
    case '070002':
      return '能源项目';
    case '070004':
      return '化工';
    case '070005':
      return '冶金';
    case '070006':
      return '建材';
    case '070007':
      return '汽车';
    case '070008':
      return '造船';
    case '070009':
      return '飞机制造';
    case '070010':
      return '机械设备制造';
    case  '070011':
      return '轻工';
    case '070012':
      return '电子信息';
    case  '070013':
      return '电力';
    case  '070015':
      return '生态环境保护';
    case  '070016':
      return '其他';
    case  '010201':
      return '经济适用房';
    case '010202':
      return '配套商品房';
    case  '010203':
      return '廉租房';
    case  '010204':
      return '公租房';
    case  '010205':
      return '普通商品房';
    case  '010206':
      return '商住楼';
    case  '010208':
      return '酒店式公寓';
    case  '100106':
      return '宾馆酒店';
    case  '100107':
      return '商业';
    case  '100108':
      return '办公';
    case  '100110':
      return '文化';
    case  '100111':
      return '教育';
    case  '100112':
      return '体育';
    case  '090010':
      return '商办';
    case  '100113':
      return '旅游';
    case  '100114':
      return '医疗卫生';
    case  '010111':
      return '养老';
    case  '010109':
      return '其它公共建筑';
    case    '010207':
      return '别墅';
    case '010303':
      return '自用办公楼';
    case   '010302':
      return '仓储物流';
    case  '010301':
      return '工业厂房';
    case '010101':
      return '宾馆酒店';
    default :
      return '无';
  }
}

/**
 * 管委会机构
 * @param arr 必须传入管委list
 * @param str
 * @returns {*}
 */

export function getGwhjg(arr, str) {
  if (Array.isArray(arr)) {
    let arr1 = [];
    let arr2 = [];
    for (let i = 0; i < arr.length; i++) {
      arr1.push(arr[i].code.toString());
      arr2.push(arr[i].name);
    }
    let indexOf = arr1.indexOf(str.toString());
    return arr2[indexOf];
  }
}

/**
 * 建设区域
 * @returns {string}
 */

export function getSzqy(id) {
  switch (id) {
    case 1:
      return '内环线内';
    case 2:
      return '内中环线间';
    case 3:
      return '中外环线间';
    case 4:
      return '外郊环线间';
    case 5:
      return '郊环线外';
    case 6:
      return '跨区域';
  }
}

/**
 * 立项方式
 * @param id
 * @returns {string}
 */

export function getLsfs(id) {
  switch (id) {
    case 1:
      return '审批';
    case 2:
      return '核准';
    case 3:
      return '备案';
    case 4:
      return '其他';
  }
}


/**
 * 立项级别
 * @param id
 * @returns {string}
 */

export function getLxjb(id) {
  switch (id) {
    case 1:
      return '国家部委或中央单位';
    case 2:
      return '市级机关或市级单位';
    case 3:
      return '区级机关或区级单位';
    case 4:
      return '市级委托管委会';
    case 5:
      return '区级委托管委会';
    case 6:
      return '其他';
  }
}

/**
 * 建设性质
 * @param id
 * @returns {string}
 */

export function getJsxz(id) {
  switch (id) {
    case 1:
      return '新建';
    case 2:
      return '扩建';
    case 3:
      return '改建';
    case 4:
      return '其他';
  }
}

/**
 * 建设单位性质
 * @param arr
 * @param id
 * @returns {*}
 */

export function getJsdwxz(arr, id) {
  if (Array.isArray(arr)) {
    let arr1 = [];
    let arr2 = [];
    for (let i = 0; i < arr.length; i++) {
      arr1.push(arr[i].id.toString());
      arr2.push(arr[i].jsdwxz);
    }
    let indexOf = arr1.indexOf(id.toString());
    return arr2[indexOf];
  }
}

/**
 * 规划用地性质
 * @param arr
 * @param arrCode
 * @returns {*}
 */

export function getSyxz(arr, arrCode) {
  let _str = '';
  if (Array.isArray(arr)) {
    let arr1 = [];
    let arr2 = [];
    for (let i = 0; i < arr.length; i++) {
      arr1.push(arr[i].code);
      arr2.push(arr[i].name);
    }
    for (let i = 0; i < arrCode.length; i++) {
      let indexOf = arr1.indexOf(arrCode[i]);
      _str += '<span style="display: inline-block;height:30px;color:#409eff;font-size:12px;min-width:50px;border:1px solid rgba(53,139,238,0.23);background:rgba(64,158,255,.1);padding:0 10px;border-radius:4px;margin:4px 5px;">' + arr2[indexOf] + '</span>';
    }
    return _str;
  }
}

/**
 * 获取街道
 * @param arr
 * @param streetcode
 * @returns {*}
 */

export function getJd(arr, streetcode) {
  if (Array.isArray(arr)) {
    let arr1 = [];
    let arr2 = [];
    for (let i = 0; i < arr.length; i++) {
      arr1.push(arr[i].streetcode);
      arr2.push(arr[i].streetname);
    }
    let indexOf = arr1.indexOf(streetcode);
    return arr2[indexOf];
  }
}

/**
 * 用地性质
 * @returns {string}
 */

export function getYdxz(id) {
  switch (id) {
    case 1:
      return '居住';
    case 2:
      return '工业';
    case 3:
      return '仓储';
    case 4:
      return '公共设施';
    case 5:
      return '市政设施';
    case 6:
      return '其他';
  }
}

export function diffArray(inputArray, baseArray) {
  let a = [];
  let b = [];

  for (let i = 0; i < baseArray.length; i++) {
    a[baseArray[i]] = true;
  }

  for (let j = 0; j < inputArray.length; j++) {
    if (!a[inputArray[j]]) {
      b.push(inputArray[j]);
    }
  }
  return b;
}

/**
 * 部门审批小状态
 * @param stage
 * @returns {string}
 */

export function getDesignSmall(stage) {
  switch (stage) {
    case 1:
      return "待接收";
    case 2:
      return "已接收";
    case 3:
      return "待补正";
    case 4:
      return "补正待接收";
    case 5:
      return "不受理";
    case 6:
      return "待受理";
    case 7:
      return "已受理";
    case 8:
      return "暂停";
    case 9:
      return "退回修改";
    case 10:
      return "退回修改待接收";
    case 11:
      return "结果录入通过";
    case 12:
      return "结果录入不通过";
  }
}

/**
 * 权限类型
 * @param type
 * @returns {string}
 */

export function templateType(type){
  switch (type) {
    case '01':
      return "规划部门";
    case '02':
      return "建设部门";
    case '03':
      return "并联部门";
    case '04':
      return "告知部门";
    case '05':
      return "掘路部门";
    case '99':
      return "管委会";
  }
}


import moment from "moment";
import {http} from "./axios";

export default {

  dateFormat(row, column) {
    let date = row[column.property];
    if (!date || date === undefined) {
      return "";
    }
    return moment(date).format("YYYY-MM-DD HH:mm:ss");
  },

  downloadFile(index) {
    http('file', "fileserver/downLoadFile/" + index.verId, {}).then(data => {
      if ('download' in document.createElement('a')) {
        const elink = document.createElement('a');
        elink.download = index.fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(data.data);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href);
        document.body.removeChild(elink);
      } else {
        navigator.msSaveBlob(data.data, index.fileName);
      }
    })
  }
}


/**
 * 查看PDF
 * @param row
 * @param role 0 其他 1 审图 2 设计
 * @param status 1 可批注 2 查看
 */

Vue.prototype.previewPDF = function (row, role, status) {
  console.log(row, role, status);
  let isPDF = row.fileName.substr(-3);
  if (isPDF.toUpperCase() === 'PDF') {
    let r = role || 0;
    let s = status || 2;
    window.open(getPath('pdf_path') + "pc/index.html?verId=" + row.verId + "&userAccount=" + 'ceshi' + "&orgId=" + row.orgId + "&role=" + r + "&status=" + s);
  } else {
    this.$message({type: 'warning', message: '文件格式不正确，只支持pdf文件！'});
  }
};
